package com.cet.algorithm.二叉树.二叉搜索树中的搜索;

/**
 * @program: cet-practice
 * @description: BST
 * @author: 陈恩涛
 * @create: 2023-04-05 11:11
 **/
public class LC700 {

    public static void main(String[] args) {
        TreeNode root = new TreeNode(4, new TreeNode(2, new TreeNode(1), new TreeNode(3)), new TreeNode(7));
    }

    /**
     * 二叉搜索树搜索，递归
     * @param root bst root node
     * @param target search node value
     * @return subTree root node
     */
    static TreeNode bstRecurSearch(TreeNode root, int target) {
        if (root == null || root.val == target) {
            return root;
        }
        if (root.val > target) {
            return bstRecurSearch(root.left, target);
        } else {
            return bstRecurSearch(root.right, target);
        }
    }

    /**
     * 二叉搜索树搜索，迭代
     *  @param root bst root node
     *  @param target search node value
     *  @return subTree root node
     */
    static TreeNode bstIterateSearch(TreeNode root, int target) {
        while (root != null) {
            if (root.val == target) {
                return root;
            }else if (root.val > target) {
                root = root.left;
            } else{
                root = root.right;
            }
        }
        return root;
    }

}
